//
//  CommentModelDataSource.swift
//  travelMapMvvm-swift2
//
//  Created by green on 15/11/4.
//  Copyright © 2015年 travelMapMvvm-swift2. All rights reserved.
//

import Foundation
import ReactiveCocoa
import AFNetworking
import JSONHelper
import TMCommonLib

public class CommentModelDataSource: CommentModelDataSourceProtocol {
    
    public class func shareInstance()->CommentModelDataSourceProtocol{
        struct YRSingleton{
            static var predicate:dispatch_once_t = 0
            static var instance:CommentModelDataSource? = nil
        }
        dispatch_once(&YRSingleton.predicate,{
            YRSingleton.instance=CommentModelDataSource()
        })
        return YRSingleton.instance!
    }
    
    // MARK: - 查询目的地评论列表
    
    public func queryPOICommentList(pageIndex: Int, pageSize: Int, poiId: String ,poiType:Int) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber:RACSubscriber!) -> RACDisposable! in
            
            let parameters: [String:AnyObject] = ["pageIndex":pageIndex,"pageSize":pageSize,"poiId":poiId, "poiType":poiType]
            
            let manager = AFHTTPRequestOperationManager()
            manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html","text/plain"]) as Set<NSObject>
            
            manager.GET(kURL+"getPOIComments", parameters: parameters, success: { (operation, responseObj) -> Void in
                
                let resultDic = responseObj as! [NSObject:AnyObject]
                
                let success = resultDic[kSuccess] as? Int
                let msg     = resultDic[kMsg] as? String
                
                if success == 0 {
                    
                    var commentList:[CommentModel]?
                    
                    commentList <-- resultDic[kData]
                    
                    subscriber.sendNext(commentList)
                    subscriber.sendCompleted()
                    
                } else {
                    
                    subscriber.sendError(DataError.QueryPOICommentList(msg).error)
                }
                
            }) { (operation, error) -> Void in
                
                subscriber.sendError(DataError.QueryPOICommentList(nil).error)
            }
            
            return nil
        })
    }
    
    // MARK: - 新增评论
    
    public func addPOIComment(poiId: String, content: String, level: Int,userId:Int,poiType:Int) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber:RACSubscriber!) -> RACDisposable! in
            
            let parameters: [String:AnyObject] = ["content":content,"level":level,"poiId":poiId,"userId":userId,"poiType":poiType]
            
            let manager = AFHTTPRequestOperationManager()
            manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html","text/plain"]) as Set<NSObject>
            
            manager.POST(kURL+"savePoiComment", parameters: parameters, success: { (operation, responseObj) -> Void in
                
                let resultDic = responseObj as! [NSObject:AnyObject]
                
                let success = resultDic[kSuccess] as? Int
                let msg     = resultDic[kMsg] as? String
                
                if success == 0 {
                    
                    subscriber.sendNext(kSuccess)
                    subscriber.sendCompleted()
                    
                } else {
                    
                    subscriber.sendError(DataError.AddPOIComment(msg).error)
                }
                
            }) { (operation, error) -> Void in
                
                subscriber.sendError(DataError.AddPOIComment(nil).error)
            }
            
            return nil
        })
    }
}
